package com.igroupes.ovineherd.model.attribute.text;

import cn.hutool.core.convert.Convert;
import cn.hutool.json.JSONObject;
import com.igroupes.ovineherd.entity.DataModelAttributeEntity;
import com.igroupes.ovineherd.enums.BooleanKey;
import com.igroupes.ovineherd.model.IParentModelData;
import com.igroupes.ovineherd.model.Style.PasswordStyle;
import com.igroupes.ovineherd.model.Style.TextStyle;
import com.igroupes.ovineherd.model.dto.FieldApiResponse;
import com.igroupes.ovineherd.model.enums.MySqlTypeKey;
import com.igroupes.ovineherd.query.FieldQuery;
import com.igroupes.ovineherd.query.ModelDataJsonAttributeQuery;
import lombok.Data;
import lombok.EqualsAndHashCode;
import lombok.ToString;
import org.springframework.util.DigestUtils;

import java.util.List;

/**
 * 密码
 */
@EqualsAndHashCode(callSuper = true)
@Data
@ToString(callSuper = true)
public class PasswordModelData extends TextBasicAttribute{
    private static Integer LENGTH = 30;
    private static Integer DATA_LENGTH = 40;
    private static String LABEL = "盐值";
    private static String DESC = "md5使用盐值两次加密不支持解密";
//    private static String REGEX = "^(?=.*\\d)(?=.*[a-z])(?=.*[A-Z]).{8,20}$"; //必须包含大小写字母和数字的组合，可以使用特殊字符，长度在8-20之间
    private TextStyle salt;

    public PasswordModelData() {
        super();
        this.salt = new TextStyle(LABEL, "", true, LENGTH,DESC);
    }

    public Boolean verifyField(PasswordModelData json) {
        return super.verifyField(json);
    }
    public Boolean verifyFieldV2(PasswordModelData basic) {
        super.verifyFieldV2(basic);
        new TextStyle().verify(new TextStyle(LABEL, basic.getSalt().getValue(), true, LENGTH,DESC));
        return true;
    }

    @Override
    public List<FieldQuery> sqlField(IParentModelData parentModelData, DataModelAttributeEntity dataModelAttributeEntityOld) {
        PasswordModelData basicAttribute = (PasswordModelData) parentModelData.getAttributes();
        List<FieldQuery> fieldQueries = super.sqlField(parentModelData,dataModelAttributeEntityOld);
        FieldQuery fieldQuery = fieldQueries.get(0);
        fieldQuery.setType(MySqlTypeKey.VARCHAR.getCode());
        fieldQuery.setLength(DATA_LENGTH);

        DataModelAttributeEntity dataModelAttributeEntity = fieldQuery.getDataModelAttributeEntity();
        JSONObject jsonObject = dataModelAttributeEntity.getJson();
        jsonObject.set("salt", Convert.toStr(basicAttribute.getSalt().getValue()));
        dataModelAttributeEntity.setJson(jsonObject);
        fieldQuery.setDataModelAttributeEntity(dataModelAttributeEntity);
        fieldQueries.set(0,fieldQuery);
        return fieldQueries;
    }
    @Override
    public JSONObject getAttribute(ModelDataJsonAttributeQuery modelDataJsonAttributeQuery) {
        JSONObject jsonObject = super.getAttribute(modelDataJsonAttributeQuery);
        jsonObject.set("salt", new TextStyle(LABEL, modelDataJsonAttributeQuery.getJson().get("salt"), true, LENGTH,DESC));
        return jsonObject;
    }

    @Override
    public List<FieldApiResponse> verifyRequestData(DataModelAttributeEntity modelAttributeEntity, Object value) {
        List<FieldApiResponse> fieldApiResponses = super.verifyRequestData(modelAttributeEntity, value);
        if(value == null){
            return fieldApiResponses;
        }

        JSONObject jsonObject = modelAttributeEntity.getJson();
        String salt = (String) jsonObject.get("salt");
        String data = (String) value;
        String base = data +"/"+salt;
        String md5 = DigestUtils.md5DigestAsHex(base.getBytes());
        FieldApiResponse fieldApiResponse = fieldApiResponses.get(0);
        fieldApiResponse.setDbName(modelAttributeEntity.getDbName());
        fieldApiResponse.setValue(md5);
        fieldApiResponse.setIsStr(true);
        fieldApiResponse.setType(MySqlTypeKey.VARCHAR.getCode());
        fieldApiResponses.set(0,fieldApiResponse);
        return fieldApiResponses;
    }
    @Override
    public JSONObject getAttributeStyle(ModelDataJsonAttributeQuery dataModelAttributeEntity) {

        return new JSONObject(new PasswordStyle(
                dataModelAttributeEntity.getName(),"",
                dataModelAttributeEntity.getIsNull().equals(BooleanKey.TRUE.getCode())
        ), false);
    }

}
